# -*- coding: utf-8 -*-

def make_link_html(url, text)
  return "<a href=\"#{url}\">#{text}</a>"
end

def make_select_form(name, values, default)
  
  html = ""
  
  values.each { |key, val|
    #print "#{key}, #{val}, #{default}, #{key==default}<br>"
    if key == default then
      opt = " selected"
    else
      opt = ""
    end
    html += "<option value='#{CGI.escapeHTML(key)}'#{opt}>#{CGI.escapeHTML(val)}</option>\n"
  }
  
  html = "<select name='#{name}'>#{html}</select>"
  
  return html
  
end

def make_yes_no_form(name, default)
  
  values = {
    "true" => $msg_yes,
    "false" => $msg_no
  }
  
  return make_select_form(name, values, default)
  
end

def make_select_form_by_array(name, values, default)
  
  html = ""
  
  values.each { |row|
    #print "#{key}, #{val}, #{default}, #{key==default}<br>"
    key = row["id"]
    val = row["val"]
    if key == default then
      opt = " selected"
    else
      opt = ""
    end
    html += "<option value='#{CGI.escapeHTML(key)}'#{opt}>#{CGI.escapeHTML(val)}</option>\n"
  }
  
  html = "<select name='#{name}'>#{html}</select>"
  #html += default
  return html
  
end

def make_select_form_without_key(name, values, default)
  
  html = ""
  flg = false
  
  values.each { |val|
    if val != nil then
      if val == default then
        opt = " selected"
        flg = true
      else
        opt = ""
      end
      html += "<option value='#{CGI.escapeHTML(val.to_s)}'#{opt}>#{CGI.escapeHTML(val.to_s)}</option>\n"
    end
  }
  
  if !flg then
    html += "<option value='#{CGI.escapeHTML(default.to_s)}' selected>#{CGI.escapeHTML(default.to_s)}</option>\n"
  end
  
  html = "<select name='#{name}'>#{html}</select>"
  
  return html
  
end

def make_button_form_with_hidden(name, values)
  
  html = "\n<form method=\"post\">"
  values.each do |key, val|
    html += "  <input type=\"hidden\" name=\"#{key}\" value=\"#{CGI.escapeHTML(val)}\" />\n"
  end
  html += "<input type=\"submit\" name=\"submit\" value=\"#{name}\" />\n"
  html += "</form>\n"
  return html
  
end

def make_date_form(name, default, year, span)
  
  val_y = []
  val_m = []
  val_d = []
  
  year.upto(year+span) {|y|
    val_y[y] = y
  }
  
  1.upto(12) {|m|
    val_m[m] = m
  }
  
  1.upto(31) {|d|
    val_d[d] = d
  }
  
  html  = make_select_form_without_key(name + "_y", val_y, default.year) + "年"
  html += make_select_form_without_key(name + "_m", val_m, default.month) + "月"
  html += make_select_form_without_key(name + "_d", val_d, default.day) + "日"
  return html
  
end

def get_date_from_form(name, values)
  
  y = values["#{name}_y"]["val"]
  m = values["#{name}_m"]["val"]
  d = values["#{name}_d"]["val"]
  
  begin
    t = Date.new(y.to_i, m.to_i, d.to_i)
  rescue
    t = Date.today()
  end
  
  values.delete(name + '_y')
  values.delete(name + '_m')
  values.delete(name + '_d')
  
  return t
  
end

def get_year_select_form(start, default)
  
  year = start.to_i
  val = []
  
  year.upto(Date.today.year) {|y|
    val[y] = y.to_s
  }
  
  html  = make_select_form_without_key("year", val, default)
  return html
  
end
  
def nl2br(txt)
  return txt.gsub(/\n/,"<br />")
end

def getQueries()
 array = ENV["QUERY_STRING"].split("&")
 hash = Hash[]
 array.each do |i|
  hash.store i.split("=")[0],i.split("=")[1]
 end
 return hash
end

def GetDateString(val)
  
  d = Date.strptime(val, "%Y-%m-%d")
  
  y = d.year
  m = d.month
  d = d.day
  
  if y < 1989 then
    g = "S"
    y = y - 1925
    tmp = sprintf("%s%02d.%02d.%02d",g,y,m,d)
  elsif y == 1989 then
    tmp = sprintf("H元.%02d.%02d",m,d)
  else
    g = "H"
    y = y-1988
    tmp = sprintf("%s%02d.%02d.%02d",g,y,m,d)
  end
  
  #return "H#{y}.#{m}.#{d}"
  return tmp
  
end

def GetDateString2(val)
  
  dt = Date.strptime(val, "%Y-%m-%d")
  
  y = dt.year
  m = dt.month
  d = dt.day
  
  if dt < Date::new(1989, 1, 8) then
    g = "S"
    y = y - 1925
    tmp = sprintf("%s%02d.%02d",g,y,m)
  elsif y == 1989 then
    tmp = sprintf("H元.%02d",m)
  else
    g = "H"
    y = y-1988
    tmp = sprintf("%s%02d.%02d",g,y,m)
  end
  
  #return "H#{y}.#{m}.#{d}"
  return tmp
  
end

def GetYear(dt)
  
  m = dt.month
  y = dt.year
  
  if m < 4 then
    y -= 1
  end
  
  return y
  
end